<?php
require_once "weixin_info.php";//å¾®ä¿¡æŽˆæƒèŽ·å–ç”¨æˆ·å¤´åƒç­‰ä¿¡æ?
require_once "config.php";
$shop_son_id = empty($_GET['shop_son_id']) ? $_SESSION['Member_info'][0]['id'] : $_GET['shop_son_id'];
if(!empty($_GET['way'])){
    $way = $_GET['way'];
    $id = $_GET['id'];
    // echo $id;exit;
    if(!empty($id)){
        $goods_sql = $mysql->query("select * from dyspw_goods where id=".$id);
        $goods = $goods_sql->fetch_all(MYSQLI_ASSOC);
        $info_img = explode(",",$goods[0]['info_img']);
        $info_img = array_filter($info_img);
        $info = explode(",",$goods[0]['info']);
        $info = array_filter($info);
        $goods_info = array();
        foreach ($info_img as $key => $val) {
            $goods_info[$key]['img'] = $val;
            $goods_info[$key]['info'] = $info[$key];
        }
        // print_r($goods_info);exit;
    }else{
        Header("location:mem_goods.php");exit();
    }
}
// print_r($_SESSION['Member_info']);exit;
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport"content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title>添加商品</title>
    <link href="css/mui.min.css" rel="stylesheet" />
    <link href="css/goods-edit.css" rel="stylesheet" />
    <script src="js/mui.min.js"></script>
    <script src="js/jquery-2.2.4.min.js" type="text/javascript"></script>
    <script src="js/jquery.cookie.js" type="text/javascript"></script>
    <script src="js/layer/layer.js" type="text/javascript"></script>
    <script src="js/clipboard.min.js"></script>
    <style media="screen">
        .goods_info{    border: 0px !important;position: relative;top: -5px;font-size: 14px;margin-bottom: 0px !important;}
    </style>
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?cb5d49b07e0a753dc8ee27ba2c73495d";
  var s = document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(hm, s);
})();
</script>
</head>
<body>
    <div class="container">
        <div class="top">
            <div class="imgUploadBox" >
                <div class="uploadBox" style="width:100%;">
                    <div class="round" style="<?php if(!empty($goods)){echo "display:none";} ?>">+</div>
                    <div class="placeholder" style="<?php if(!empty($goods)){echo "display:none";} ?>" data-type="goods_img">选择图片</div>
                    <img class="goods_img" style="width:100%;" src="<?php if(!empty($goods)){echo $goods[0]['img'];} ?>" alt="">
                    <input type="hidden" name="goods_img" value="<?php if(!empty($goods)){echo $goods[0]['img'];} ?>">
                </div>
            </div>
            <img src="" alt="">
            <div class="proInfo mui-input-row">
                <input type="number" class="mui-input-clear" name="money" value="<?php if(!empty($goods)){echo $goods[0]['money'];} ?>" placeholder="添加商品单价">
                <input type="number" class="mui-input-clear" name="group_money" value="<?php if(!empty($goods)){echo $goods[0]['group_money'];} ?>" placeholder="添加商品团购价格">
                <input type="number" class="mui-input-clear" name="stock" value="<?php if(!empty($goods)){echo $goods[0]['stock'];} ?>" placeholder="添加商品库存">
                <input type="number" class="mui-input-clear" name="sales" value="<?php if(!empty($goods)){echo $goods[0]['sales'];} ?>" placeholder="添加商品已售数量">
                <input type="text" class="mui-input-clear" name="title" value="<?php if(!empty($goods)){echo $goods[0]['title'];}?>" placeholder="添加商品名称">
                <label for="rob" style="    display: block;width: 100%;"><input name="rob" id="rob" type="checkbox" value="rob" /><span style="font-size: 14px;color: #929292;display: inline-block;position: relative;top: -14px;left: 5px;">抢购商品</span></label>
                <textarea placeholder="添加商品简介(不多于100字)" name="desc" maxlength="100" id="" cols="30" rows="3" class="" style="display: block;width: 96%;"><?php if(!empty($goods)){echo $goods[0]['details'];} ?></textarea>
            </div>
            <!-- <div class="band">
                <div class="bandInfo ">
                    <div class="logo">
                        <div class="round">+</div>
                    </div>
                    <div class="compName">
                        <div class="name">大自然官方旗舰店</div>
                        <div class="zan">5.5万人点赞</div>
                    </div>
                </div>
                <div class="btns">
                    <button type="button" class="mui-btn connect">联系客服</button>
                    <button type="button" class="mui-btn ">进店逛逛</button>
                </div>
            </div> -->
        </div>
        <div class="bottom top" style="padding-top:0px; margin-bottom50px;">
            <ul class="goods_ils" style="padding: 0px;margin: 0px;margin-bottom: 5px;">
                <?php if(!empty($goods_info)){ foreach ($goods_info as $key => $val) {?>
                    <li style="list-style: none;position:relative;">
                        <img style="width:100%;" src="<?php echo $val['img'] ?>" alt="">
                        <img class="del_info" style="position: absolute;top: 10px;width: 30px;right: 10px;" src="images/delite.png" alt="">
                        <input type="hidden" class="goods_img" name="goods_img'+length+'" value="<?php echo $val['img'] ?>">
                        <textarea class="goods_info" type="text" name="info" value="" placeholder="添加商品详情"><?php echo $val['info'] ?></textarea>
                    </li>
                <?php }} ?>
            </ul>
            <div class="proInfo mui-input-row add_info"style="margin-bottom:50px;">
                <div class="imgUploadBox" >
                    <div class="uploadBox">
                        <div class="round">+</div>
                        <div class="placeholder" data-type='goods_ils'>选择图片</div>
                    </div>
                </div>
                <textarea class="" type="text" name="introduce" value="" placeholder="添加商品详情介绍"><?php if(!empty($goods)){echo $goods[0]['introduce'];} ?></textarea>
            </div>
            <!-- <form action="update.php" target="exec_target" method="POST" enctype="multipart/form-data" style="display:none;">
				<input type="hidden" name="FilePath" value="Goods/Thumb" />
				<input class="img_file" name="face" type="file" data-type='' multiple=true value="" >
				<input type="submit" id="PicSubmit"/>
			</form>
			<iframe id="exec_target" name="exec_target" style="display:none"></iframe> -->
            <!-- <div class="detailInput mui-input-row">
                <input type="text" class="mui-input-clear" name="info" value="<?php if(!empty($goods)){echo $goods[0]['info'];} ?>"  placeholder="添加商品详情介绍">
            </div> -->
            <div class="btnBox">
                <button type="button" class="mui-btn cancelBtn" ><a href="member.php" style="color:#fff;">取消</a></button>
                <button type="button" class="mui-btn comfirmBtn" style="color:#fff;">上传</button>
            </div>
        </div>
        <input type="file" id="file" class="filepath" onchange="changepic(this)"
            accept="image/jpg,image/jpeg,image/png,image/PNG" data-type="" hidden/>

        <canvas id="canvas" style="display: none;"></canvas>
    </div>
    <script src="./js/compress.js"></script>
    <script type="text/javascript">
    $(".imgUploadBox").click(function(){
        var data_type = $(this).find('.placeholder').attr('data-type');
        $("#file").attr('data-type',data_type);
        $('#file').click();
    })

    function changepic(obj) {
        if (!obj.files || !obj.files[0]) {
            return;
        }
        var form = new FormData();
        var fileObj = obj.files[0];
        if(fileObj.size/1024 > 1025) {
            photoCompress(fileObj, {quality: 0.2}, function(base64Codes){
                var bl = new window.File([convertBase64UrlToBlob(base64Codes)], 'test.jpeg', {
                            type: 'image/jpg,image/jpeg,image/png,image/PNG'
                        });
                form.append("file", bl);
                sendToSever(form);
            })
        } else {
            form.append("file", fileObj)
            sendToSever(form);
        }
    }

            function getFileName(o) {
                var pos = o.lastIndexOf("\\");
                return o.substring(pos + 1);
            }

            function sendToSever(formData) {
                $.ajax({
                    url: 'fileup_img.php',
                    type: "post",
                    data: formData,
                    dataType: 'json',
                    processData: false, // 使数据不做处理
                    contentType: false, // 不要设置Content-Type请求头
                    timeout: 10000,
                    beforeSend: function () {
                        lid = layer.open({
                            type: 2,
                            content: '提交中...',
                            shadeClose: false
                        });
                    },
                    success: function (res) {
                        if (res.status == 'y') {
                            layer.open({
                                content: res.info,
                                skin: 'msg',
                                time: 2 //2秒后自动关闭
                            });
                        } else {
                            layer.open({
                                content: res.info,
                                skin: 'msg',
                                time: 2 //2秒后自动关闭
                            });
                        }
                        // _img = "https://5p8.fyh5p8.com/xiaochengxu/"+res.src;
                        _img = res.src;
                        var img_type = $("#file").attr('data-type');
                        if(img_type == 'goods_img'){
                            $(".goods_img").attr('src', _img);
                            $("input[name='goods_img']").val(_img);
                            $(".goods_img").parent().find('div').hide();
                        }else{
                            html = '<li style="list-style: none;position:relative;""><img  style="width:100%;" src="'+_img+'" alt=""><img class="del_info" style="position: absolute;top: 10px;width: 30px;right: 10px;" src="images/delite.png" alt=""><input type="hidden" class="goods_img" name="goods_img" value="'+_img+'"><textarea class="goods_info" type="text" name="info" value="" placeholder="添加商品详情介绍"></textarea></li>';
                            // html = '<li style="list-style: none;position:relative;""><img class="del_info" style="width:100%;" src="'+_img+'" alt=""><img style="position: absolute;top: 10px;width: 30px;right: 10px;" src="images/delite.png" alt=""><input type="hidden" class="goods_img" name="goods_img" value="'+_img+'"></li>';
                            $(".goods_ils").append(html);
                        }
                        var length = $(".goods_ils li").length;
                        if(length > 8){
                            $(".add_info").hide();
                        }
                        // console.log(_img);
                        // return;
                        // console.log(_img);
                    },
                    complete: function () {
                        layer.close(lid);
                    },
                    error: function (XMLHttpRequest, textStatus) {
                        layer.close(lid);
                        if ('timeout' == textStatus) {
                            alert('请求接口超时！');
                        } else {
                            alert(JSON.stringify(XMLHttpRequest))
                        }
                    }
                });
            }

        // $(".imgUploadBox").click(function(){
        //     var data_type = $(this).find('.placeholder').attr('data-type');
        //     $(".img_file").attr('data-type',data_type);
        //     $(".img_file").click();
        //     $(".img_file").attr('data-type', data_type);
        // })
        // $(".img_file").change(function(){
        //     var img_type = $(this).attr('data-type');
        //     $('#PicSubmit').click();
        //     var html = '';
        //     var count = 0;
        //     $("#exec_target").load(function(){
        //         var data = $(window.frames['exec_target'].document.body).html();
        //         if(data != ''){
        //             count++;
        //             if(count <= 1){
        //                 if(img_type == 'goods_img'){
        //
        //                     $(".goods_img").attr('src', "https://5p8.fyh5p8.com/xiaochengxu/"+data);
        //                     $("input[name='goods_img']").val(data);
        //                     $(".goods_img").parent().find('div').hide();
        //                 }else{
        //
        //                     var length = $(".goods_ils > li").length;
        //                     var src = $(".goods_ils").last().find('input').val();
        //                     if(src != data){
        //                         html = '<li style="list-style: none;position:relative;""><img class="del_info" style="width:100%;" src="https://5p8.fyh5p8.com/xiaochengxu/'+data+'" alt=""><img style="position: absolute;top: 10px;width: 30px;right: 10px;" src="images/delite.png" alt=""><input type="hidden" class="goods_img" name="goods_img'+length+'" value="'+data+'"></li>';
        //                         $(".goods_ils").append(html);
        //                     }
        //                     if(length >= 9){//上传图片条数限制
        //                         $(".imgUploadBox").hide();
        //                     }
        //                 }
        //             }
        //         }
        //     });
        //     count = 0;
        //
        // })
        //
        //删除已上传的图片
        $(document).on("click", ".del_info", function(){
            $(this).parent().remove();
        });
        function is_money(money){
            if (!/^(?:[1-9][0-9]*(?:\.[0-9]{1,2})?|0(?:\.[0-9]{1,2})?)$/.test(money)) {
                return false;
			}
        }
        //表单提交
        $(".comfirmBtn").click(function(){
            var goods_ils  = '';
            var goods_info = '';
            $(".goods_ils > li").each(function(){
                goods_ils = goods_ils +','+ $(this).find('input').val();
                goods_info = goods_info +','+ $(this).find('textarea').val();
            })
            goods_ils = goods_ils.replace(' ', '');
            goods_info = goods_info.replace(' ', '');

            // console.log(goods_ils, goods_info);
            var title = $("input[name='title']").val();
            var goods_img = $("input[name='goods_img']").val();
            var rob = $("input[name='rob']:checked").val();
            var money = $("input[name='money']").val();
            var group_money = $("input[name='group_money']").val();
            var stock = $("input[name='stock']").val();
            var sales = $("input[name='sales']").val();
            var details = $("textarea[name='desc']").val();
            var info = $("input[name='info']").val();
            var introduce = $("textarea[name='introduce']").val();
            var u_id = '<?php if(!empty($shop_son_id)){echo $shop_son_id;}else{echo $_SESSION['Member_info'][0]['id'];}  ?>';
            if (!/^(?:[1-9][0-9]*(?:\.[0-9]{1,2})?|0(?:\.[0-9]{1,2})?)$/.test(money)) {
                layer.open({content: '商品价格只能输入数字，小数点后只能保留两位',skin: 'msg',time: 2 });//2秒后自动关闭
                return false;
			}
            if (!/^(?:[1-9][0-9]*(?:\.[0-9]{1,2})?|0(?:\.[0-9]{1,2})?)$/.test(group_money)) {
                layer.open({content: '团购价格只能输入数字，小数点后只能保留两位',skin: 'msg',time: 2 });//2秒后自动关闭
                return false;
			}

            if(goods_img == '' || title == '' || money == '' || details == '' || group_money == ''){
                layer.open({content: '以上所有信息为必填项，请重新输入',skin: 'msg',time: 2 });//2秒后自动关闭
                return false;
            }

            var way = "<?php if(!empty($way)){echo $way;} ?>";
            var id = "<?php if(!empty($goods)){echo $goods[0]['id'];} ?>";
            $.ajax({
                url: 'goods_ajax.php',
                type: "post",
                data: {
                    'title':title,
                    'goods_img':goods_img,
                    'money':money,
                    'group_money':group_money,
                    'introduce':introduce,
                    'details':details,
                    'info':goods_info,
                    'u_id':u_id,
                    'info_img':goods_ils,
                    'way':way,
                    'id':id,
                    'stock':stock,
                    'rob':rob,
                    'sales':sales
                } ,
                dataType: 'json',
                success:function(res){
                    if(res.status == 'y'){
                        layer.open({content: res.info,skin: 'msg',time: 2 });
                        location.href="mem_goods.php";
                    }else{
                        layer.open({content: res.info,skin: 'msg',time: 2 });
                    }
                }
            })

        })

    </script>
</body>

</html>
